package com.gitee.wsl.compose.ui.popup

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.window.DialogProperties
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import com.gitee.wsl.compose.ui.base.Popup


@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun Popup.AnimatedPopup(
    onDismissRequest: () -> Unit,
    properties: DialogProperties = DialogProperties(),
    enterTransition: EnterTransition,
    scrimColor : Color = Color.Black.copy(alpha = .4f),
    content: @Composable BoxScope.() -> Unit
) {

    val haptic = LocalHapticFeedback.current

    Popup(
        onDismissRequest = onDismissRequest,
        properties = PopupProperties(
            dismissOnBackPress = properties.dismissOnBackPress,
            dismissOnClickOutside = properties.dismissOnClickOutside,
            //usePlatformDefaultWidth = false,
        )
    ) {

        CompositionLocalProvider(LocalHapticFeedback provides haptic) {
            var visible by remember {
                mutableStateOf(false)
            }
            LaunchedEffect(0) {
                visible = true
            }

            val animatedScrimColor by animateColorAsState(
                if (visible) scrimColor else scrimColor.copy(alpha = 0f),
            )

            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .drawWithContent {
                        drawRect(animatedScrimColor)
                        drawContent()
                    }
                    .then(
                        if (properties.dismissOnClickOutside)
                            Modifier.pointerInput(0) {
                                detectTapGestures {
                                    onDismissRequest()
                                }
                            } else Modifier
                    )
                    /*.then(
                        if (properties.platformInsets)
                            Modifier
                                .systemBarsPadding()
                                .imePadding()
                        else Modifier
                    )*/
            ) {
                AnimatedVisibility(
                    visible = visible,
                    enter = enterTransition
                ) {
                    Box(
                        modifier = Modifier
                            .fillMaxSize(),
                        content = content
                    )
                }

            }
        }
    }
}